use crate::get_reader;
use crate::Base64Format;
use base64::{
    engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD},
    prelude::*,
};

pub fn process_encode(input: &str, format: Base64Format) -> anyhow::Result<String> {
    let mut reader = get_reader(input)?;
    let mut content = Vec::new();
    reader.read_to_end(&mut content)?;

    let encoded = match format {
        Base64Format::UrlSafe => URL_SAFE_NO_PAD.encode(content),
        Base64Format::Standard => STANDARD.encode(content),
    };

    Ok(encoded)
}
pub fn process_decode(input: &str, format: Base64Format) -> anyhow::Result<Vec<u8>> {
    let mut reader = get_reader(input)?;

    let mut content = String::new();
    reader.read_to_string(&mut content)?;

    let decoded = match format {
        Base64Format::UrlSafe => URL_SAFE_NO_PAD.decode(content.trim())?,
        Base64Format::Standard => STANDARD.decode(content.trim())?,
    };

    Ok(decoded)
}
